﻿using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class AgencyIncomeDAL:BaseDAL
    {
        public AgencyIncome Get(int id)
        {
            return CONN_TopicSharing.Get<AgencyIncome>(id);
        }

        public bool Insert(AgencyIncome user)
        {
            return CONN_TopicSharing.Insert<AgencyIncome>(user);
        }

        public bool Update(AgencyIncome user)
        {
            return CONN_TopicSharing.Update<AgencyIncome>(user);
        }

        /// <summary>
        /// 申请提现
        /// </summary>
        /// <param name="au_id"></param>
        /// <returns></returns>
        public bool ApplyWithdraw(int au_id, string wd_type)
        {
            string sql = " update  [AgencyIncome] set is_apply_pay=1,apply_pay_time=getdate() where  au_id=@au_id and is_apply_pay=0;update  [Withdraw] set is_default=0 where au_id=@au_id and is_default=1;update  [Withdraw] set is_default=1 where au_id=@au_id and type=@wd_type";
            return CONN_TopicSharing.Excute(sql, new { au_id = au_id, wd_type = wd_type }) > 0;


        }

        //获取可提现金额
        public decimal GetCanApplyIncome(int au_id)
        {
            string sql = " select  isnull(sum(income+lower_income+onthe_lower_income),0.00)  from  [AgencyIncome] where au_id=@au_id and is_apply_pay=0 ";

            return CONN_TopicSharing.SQLQuery<decimal>(sql, new { au_id = au_id }).FirstOrDefault();

        }

        public List<AgencyIncome> ListPage(string strWhere, string orderbys, int pagesize, int pageindex, out int count,object param)
        {
            List<AgencyIncome> list = new List<AgencyIncome>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " 1=1 ";
            }

            if (string.IsNullOrEmpty(orderbys))
                orderbys = " Id desc ";

            list = CONN_TopicSharing.SQLPager<AgencyIncome>(" id, au_id, au_name, income_date, income, lower_income, onthe_lower_income, is_pay, is_apply_pay, createtime, apply_pay_time, pay_time ", " AgencyIncome ", strWhere, "", "", orderbys, pagesize, pageindex, out count,param).ToList();

            return list;
        }




    }
}
